// pages/shopCar/shopCar.js
import {openSetting,chooseAddress,getSetting} from "./common/method"
import {two} from "../../network/request"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  oprationIt(){
    let car = wx.getStorageSync('car') || []
    let selectAll = false
    let sum = 0
    let settle = 0
    if(car.length){
      selectAll = car.every(v=>v.chooseIt)
      car.map(v=>{
        if(v.chooseIt){
          sum += v.goods_price*v.num
          settle += v.num
        }
      })
    }
    this.setData({
      car,
      selectAll,
      sum,
      settle
    })
  },

  tmpCar:{

  },

  chooseChange(e){
    let id = e.currentTarget.dataset.chooseid
    let index = this.data.car.findIndex(v=>v.goods_id===id)
    this.tmpCar[index].chooseIt = !this.tmpCar[index].chooseIt
    this.cunData()
  },

  cunData(){
    this.setData({
      car: this.tmpCar
    })
    wx.setStorageSync('car',this.data.car)
    this.oprationIt()
  },

  calculate(e){
    let {id,num} = e.currentTarget.dataset
    let index = this.tmpCar.findIndex(v=>v.goods_id===id)
    if(this.tmpCar[index].num>0 || num==="1"){
      this.tmpCar[index].num += parseInt(num)
      let sum = 0
      let settle = 0
      this.tmpCar.map(v=>{
        if(v.chooseIt){
          sum += v.goods_price*v.num
          settle += v.num
        }
      })
      this.setData({
        car: this.tmpCar,
        sum,
        settle
      })
    }
    
    
  },

  loginIt(){
    return new Promise((rev,rej)=>{
      wx.login({
        timeout: 500,
        success(e){
          rev(e)
        }
      })
    })
  },

  async nitaimei(e){
    if(e.detail.signature){
      console.log('允许获得用户信息！')
      let {code} = await this.loginIt()
      this.setData({
        userInfo: e.detail
      })
      let {encryptedData,rawData,iv,signature} = this.data.userInfo
      let data = {encryptedData,rawData,iv,signature,code}
      console.log(data)
      let xxxx = await two({url:'/users/wxlogin',data,method:'post'})
      console.log(xxxx)
      }else{
        console.log('不允许获得用户信息！')
      }
    },

  reverseChoose(){
    let selectAll = !this.data.selectAll
    this.tmpCar.map(v=>v.chooseIt=selectAll)
    this.cunData()
  },

  onLoad: function (options) {
    let address = wx.getStorageSync('userAddress')
    this.setData({address})
    console.log(address)
    this.oprationIt()
    this.tmpCar = this.data.car
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  async ji(){
    let ni = await getSetting()
    let judge = ni.authSetting["scope.address"]
    if(judge || judge===undefined){
      let address = await chooseAddress()
      if(address.userName){
        this.setData({
          chooseDown: true,
          address
        })
      }
    }else{
      let tai = await openSetting()
      if(tai.authSetting["scope.address"]){
        let mei2 = await chooseAddress()
        if(mei2.userName){
          this.setData({
            chooseDown: true,
            address: mei2
          })
        }
      }
    }
  },
})